import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import { PostsModule } from './posts/posts.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from 'nestjs-config';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
require('dotenv').config();

import { RolesModule } from './roles/roles.module';

// 配置加载配置文件
const Config = ConfigModule.load(
  path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}'),
  { modifyConfigName: name => name.replace('.config', '') },
);

// 配置mysql连接
const typeOrmModule = TypeOrmModule.forRoot({
  name: 'default',
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '12345678',
  database: 'typeorm_mysql',
  synchronize: true, // 自动同步数据模型到表中
  logging: false,
  entities: [__dirname + '/**/*entity{.ts,.js}'],
});

// const typeOrmModule = TypeOrmModule.forRootAsync({
//   useFactory: async (config: ConfigService) => {
//     console.log('process.env.DB_DATABASE', process.env.DB_DATABASE);
//     const data = {
//       type: config.get('database.type'),
//       host: config.get('database.host'),
//       port: config.get('database.port'),
//       database: config.get('database.database'),
//       username: config.get('database.username'),
//       password: config.get('database.password'),
//       logging: config.get('database.logging'),
//       entities: [__dirname + '/**/*.entity{.ts,.js}'],
//       synchronize: true,
//     };
//     console.log(data);
//     return data;
//   },
//   inject: [ConfigService],
// });

@Module({
  imports: [Config, typeOrmModule, UsersModule, PostsModule, RolesModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
